package cn.buyclss.backend.service.impl.shop;

import cn.buyclss.backend.entity.domain.User;
import cn.buyclss.backend.entity.domain.shop.Shop;
import cn.buyclss.backend.entity.po.shop.ShopPO;
import cn.buyclss.backend.entity.support.RespResult;
import cn.buyclss.backend.entity.vo.shop.ShopVO;
import cn.buyclss.backend.mapper.UserMapper;
import cn.buyclss.backend.mapper.shop.ShopMapper;
import cn.buyclss.backend.service.shop.ShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: Cecurio
 * @create: 2019-03-11 22:02
 **/
@Service
public class ShopServiceImpl implements ShopService {

    private Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public RespResult getAll() {
        List<ShopPO> shopList = shopMapper.selectAll();
        System.out.println(shopList);
        RespResult res = RespResult.successInstance();
        res.putValue("shops", shopList);
        return res;
    }

    @Override
    public RespResult addOne(ShopVO shopVO) {
        RespResult res = RespResult.successInstance();

        // 1.检查用户是否已经有了商铺
        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Integer uid = 0;
        if (obj instanceof User) {
            User user = (User) obj;
            uid = user.getId();
        } else {
            return RespResult.failureInstance("系统异常，稍后重试！");
        }

        List<ShopPO> shops = shopMapper.selectUserShop(uid);
        if (shops.size() >= 1) {
            return RespResult.failureInstance("您已经有商铺了，无法再次添加！");
        }

        // 2、没有，则允许添加商铺
        Shop shop = new Shop();
        shop.setOwnId(uid);
        shop.setName(shopVO.getName());
        shop.setIntroduction(shopVO.getIntroduction());
        shop.setLogo(shopVO.getLogo());
        shop.setAddress(shopVO.getAddress());

        shop.setDeleted(0);
        shop.setRevision(1);
        shop.setGoodsCount(0);
        shop.setOrderCount(0);
        shop.setGoodMarkTimes(0);
        shop.setCreatedTime(new Date());
        shop.setUpdatedTime(new Date());

        int id = shopMapper.insertSelective(shop);
        logger.info("编号为" + uid + "的用户，新增加的店铺ID: " + shop.getId());

        // 3、修改p_user表的hasShop为1
        User updateUser = new User();
        updateUser.setId(uid);
        updateUser.setHasShop(1);
        userMapper.updateByPrimaryKeySelective(updateUser);

        return res;
    }

    @Override
    public RespResult page(Integer pageIndex, Integer pageSize) {
        RespResult res = RespResult.successInstance();
        // startPage(第几页, 多少条数据)
        PageHelper.startPage(pageIndex, pageSize);
        // Mybatis查询方法
        List<ShopPO> list = shopMapper.selectAll();
        // 用PageInfo对结果进行包装
        PageInfo<ShopPO> pageInfo = new PageInfo<ShopPO>(list);
        res.putValue("pages", pageInfo);
        return res;
    }

    @Override
    public RespResult getMyShop() {
        RespResult res = RespResult.successInstance();

        Subject subject = SecurityUtils.getSubject();
        Object obj = subject.getPrincipal();
        Integer id = 0;
        if (obj instanceof User) {
            User user = (User) obj;
            id = user.getId();
        } else {
            return RespResult.failureInstance("系统异常，稍后重试！");
        }

        List<ShopPO> shops = shopMapper.selectUserShop(id);
        res.putValue("shop", shops.get(0));
        return res;
    }
}
